<?php
class Cms_Permitsion_AclPlugin extends Zend_Controller_Plugin_Abstract {
	
	protected $_acl;
	protected $_auth;
	protected $role = "Guest";
	public function __construct($acl) {
		$this->_acl = $acl;
		$this->_auth = Zend_Auth::getInstance ();
	}
	public function preDispatch(Zend_Controller_Request_Abstract $res) {
		$controller = $res->getControllerName ();
		$action = $res->getActionName ();
		$module = $res->getModuleName ();
		$this->role = "Guest";
		$result = $this->_auth->hasIdentity ();
		if ($result) {			
			$info = $this->_auth->getIdentity ();
			$level = $info->user_group_user_id;
			switch ($level) {
				case 1 :
					$this->role = "SuperAdmin";
					break;
				case 2 :
					$this->role = "Admin";
					break;
				case 3 :
					$this->role = "Member";
					break;
				default :
					$this->role = "Guest";
			}
		}
		
		$controller = $module . ":" . $controller;
		if (! $this->_acl->isAllowed ( $this->role, $controller, $action )) {
			if ($module == "admin") {
				
				if (! $this->_auth->hasIdentity ()) {
					$res->setModuleName ( "admin" )->setControllerName ( 'user' )->setActionName ( 'login' );
				} else 
					if($this->_auth->hasIdentity ()) {
					$res->setModuleName ( "admin" )->setControllerName ( 'index' )->setActionName ( 'error' );
					$res->setParam('messsage', "Bạn không có quyền truy cập vào trang này");
				}
			}
		}
		if ($this->_request->getControllerName () == 'error'){
			
			return ;

		}
	}
}
?>